package org.itboys.seller.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.utils.io.FileUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.mongodb.utils.query.AceUtils;
import org.itboys.seller.constant.SellerConstants;
import org.itboys.seller.entity.Seller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;

/**
 * 商家
 * @author weisky
 *
 */
@Service
public class SellerService extends BaseService<Seller, Long>{
	
	private static final long serialVersionUID = 1L;
	@Resource(name = "sellerDS")
	private MongoDataSource ds;
	
	@Autowired
	private ResourceHolder resourceHolder;

	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<Seller> getEntityClass() {
		return Seller.class;
	}

	/**
	 * 根据商店名称模糊查询列表
	 * @param shopName
	 * @return
	 */
	public List<Seller> getShopListListByName(String sellerName) {
		return ds.createQuery(getEntityClass()).filter("name", AceUtils.getPattern(sellerName))
				.filter("isDeleted", SellerConstants.TYPE_ZERO).asList();
	}
	
	/**
	 * 保存或修改
	 * @param seller
	 * @param logos:商家logo
	 * @param mainImgs:商家主图
	 * @return
	 */
	public boolean insert(Seller seller, MultipartFile logos, MultipartFile mainImgs){
		try{
			
			String logo = null;
			String mainImg = null;
			
			if(logos != null && !logos.isEmpty()){
				logo = FileUtils.saveFile(logos.getInputStream(), 
						resourceHolder.getStringValue("fileUploadPath"), 
						logos.getOriginalFilename(), logos.getContentType());
				seller.setLogo(logo);
			}
			if(mainImgs != null && !mainImgs.isEmpty()){
				mainImg = FileUtils.saveFile(logos.getInputStream(), 
						resourceHolder.getStringValue("fileUploadPath"), 
						mainImgs.getOriginalFilename(), mainImgs.getContentType());
				seller.setMainImg(mainImg);
			}
			
			if(seller.getId() != 0){//修改
				Seller seller2 = getById(seller.getId());
				//设置商家的logo
				if(StringUtils.isBlank(logo)){
					seller.setLogo(seller2.getLogo());
				}
				//设置商家的主图
				if(StringUtils.isBlank(mainImg)){
					seller.setMainImg(seller2.getMainImg());
				}
				update(seller);
				logger.info("修改店铺信息成功：Seller=" + JSON.toJSONString(seller));
			}else{//新增
				save(seller);
				logger.info("新增店铺信息成功：Seller=" + JSON.toJSONString(seller));
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("SellerService.insert error", e);
			return false;
		}
	}
	
	/**
	 * 修改商家的状态
	 * @param sellerId
	 * @param state
	 * @return
	 */
	public boolean updateState(Long sellerId, int state){
		try{
			update(sellerId, "state", state);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("SellerService.updateState error", e);
			return false;
		}
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	public boolean dele(Long id){
		try{
			delete(id);
			logger.info("删除店铺成功：Seller.id=" + id + ", 删除时间 = " + new Date());
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("SellerService.dele error", e);
			return false;
		}
	}
	
}
